﻿package net.nicoptere.media.video.tracker3d 
{
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Matrix;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	
	import net.nicoptere.media.video.tracker3d.DAELoader;
	
	/**
	 * @author Nicolas Barradeau
	 */
	public class Scene extends Sprite
	{
		
		
		private var _next:Scene;
		public function set next( scene:Scene ):void { _next = scene;  }
		public function get next():Scene { return _next;  }
		
		private var sequences:Array;
		private var textures:Array;
		private var models:Array;
		private var dae:DAELoader;
		
		
		public function Scene( textures:Array, models:Array, layers:Boolean = false ) 
		{
			super();
			
			sequences = new Array();
			this.models = models.concat();
			this.textures = textures.concat();
			
		}
		
		public function load( ):void
		{
			
			//-> charge le modele
			//boucle si X models
			dae = new DAELoader();
			dae.addEventListener( Event.COMPLETE, completeHandler );
			dae.addEventListener( FileLoadEvent.LOAD_PROGRESS, progress );
			//addChild( dae );
			dae.load( models[ 0 ] );
			
		}
		
		// progression
		private function progress( e:FileLoadEvent ):void
		{
			
			if ( Tracker3D.debug )trace( 'DaeLoader.Progress :' , ( e.bytesLoaded / e.bytesTotal ) );
			dispatchEvent( e );
			
		}
		
		// <- fin de chargement 
		private function completeHandler( e:Event ):void
		{
			
			dae.removeEventListener( Event.COMPLETE, completeHandler );
			dae.removeEventListener( FileLoadEvent.LOAD_PROGRESS, progress );
			if ( Tracker3D.debug ) trace( 'DaeLoader.complete' );
			
			//crée les textures
			assignTextures();
			
			//crée les séquences
			buildSequences();
			
			dispatchEvent( e );
			
		}
		
		//assigne les textures
		public function assignTextures():void
		{
			var tex:MovieMaterial
			var _do:DisplayObject3D;
			for each ( var t:Texture in textures )
			{
				
				// retrouve les modeles 
				_do = dae.model.getChildByName( t.objectName, true );
				
				// associe les textures
				tex = new MovieMaterial( t, false, t.animated, false,  new Rectangle( 0,0,256,256 ) ) ;
				tex.doubleSided = true;
				_do.material = tex;
				
			}
		}
		
		public function buildSequences( layers: Boolean = false ):void
		{
			
			
			//rend les séquences
		
			var framecount:int = dae.totalFrames;
				
			var canvas:BitmapData = new BitmapData( Tracker3D.viewportWidth, Tracker3D.viewportHeight, true, 0x00000000 );
			var zone:BitmapData = new BitmapData( Tracker3D.viewportWidth, Tracker3D.viewportHeight, true, 0x00000000 );
			var crop:BitmapData;
			var rect:Rectangle;
			
			var frame:Frame;
			var sequence:Sequence = new Sequence();
			
			for ( var i:int = 0; i < framecount; i++ )
			{
				
				dae.model.play( String( i ), false );
				dae.singleRender();
				
				
				canvas.fillRect( canvas.rect, 0x00000000 );
				canvas.draw( dae );
				
				zone.fillRect( canvas.rect, 0x00000000 );
				zone.threshold( canvas, canvas.rect, new Point(), '!=', 0x00000000, 0xFF000000 );
				rect = zone.getColorBoundsRect(  0xFFFFFFFF, 0xFF000000 );
				
				if ( rect.width > 0 && rect.height > 0 )
				{
					
					frame = new Frame( rect );
					frame.draw( canvas, new Matrix( 1, 0, 0, 1, -rect.x, -rect.y ) );
					sequence.addFrame( frame );
					
				}
				
			}
			addChild( sequence );
			sequences.push( sequence );
		}
		
		public function gotoAndStop( frame:uint ):void 
		{
			
			for each( var s:Sequence in sequences )
			{
				s.gotoAndStop( frame );
			}
			
		}
		
		public function gotoAndPlay( frame:uint ):void 
		{
			
			for each( var s:Sequence in sequences )
			{
				s.gotoAndPlay( frame );
			}
			
		}
		
		public function play():void 
		{
			
			for each( var s:Sequence in sequences )
			{
				s.play();
			}
			
		}
	}

}